package com.ewzj.bigdata.mvp.model

import android.app.Application
import com.ewzj.bigdata.app.config.BaseApplication
import com.ewzj.bigdata.app.config.netmodel.CommonCache
import com.ewzj.bigdata.app.config.netmodel.CommonService
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.contract.HomeFragmentContract
import com.ewzj.bigdata.mvp.contract.NewFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.BaseListData
import com.ewzj.bigdata.mvp.model.rspbean.InfosData
import com.ewzj.bigdata.mvp.model.rspbean.RspOriginBean
import com.google.gson.Gson
import com.jess.arms.integration.IRepositoryManager
import com.jess.arms.mvp.BaseModel
import com.jess.arms.utils.DeviceUtils
import io.reactivex.Observable
import io.rx_cache2.DynamicKey
import io.rx_cache2.EvictDynamicKey
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class NewFragmentModel @Inject constructor(val mGson:Gson, val application: Application, repositoryManager: IRepositoryManager):BaseModel(repositoryManager),NewFragmentContract.Model{
    val service=repositoryManager.obtainRetrofitService(CommonService::class.java)
    val cache=repositoryManager.obtainCacheService(CommonCache::class.java)
    override fun getInfos(page: Reqpagebean): Observable<InfosData> {
        return cache.infoList(service.infoList(Apputils.Utils.json2Requestbody(mGson.toJson(page))).map { t: BaseData<InfosData> -> t.data }, DynamicKey(1), EvictDynamicKey(DeviceUtils.hasInternet(application)))
    }

    override fun getnewsByPageFromNet(pagebean: Reqpagebean): Observable<BaseData<BaseListData<RspOriginBean>>> {
        return service.orginTopicV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean)))
    }

    override fun getnewsByPage(pagebean: Reqpagebean): Observable<List<RspOriginBean>> {
        return cache.orginTopicV3(service.orginTopicV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean))).map { t: BaseData<BaseListData<RspOriginBean>> ->t.data!!.list  }
                , DynamicKey(pagebean.p),EvictDynamicKey(DeviceUtils.hasInternet(application)))
    }

    override fun getnewsTypeByPage(pagebean: Reqpagebean): Observable<List<RspOriginBean>> {
        return cache.orginTopicV3(service.orginTypeV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean))).map { t: BaseData<BaseListData<RspOriginBean>> ->t.data!!.list  }
                , DynamicKey(pagebean.p),EvictDynamicKey(DeviceUtils.hasInternet(application)))
    }
}